package com.ear.orange.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ear.orange.common.Result;
import com.ear.orange.dto.OrdersDto;
import com.ear.orange.entity.OrderDetail;
import com.ear.orange.entity.Orders;
import com.ear.orange.service.OrderDetailService;
import com.ear.orange.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 *
 * @author orange
 */
@Slf4j
@RestController
@Api(tags = "用户订单")
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return Result.success("下单成功");
    }

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 个人中心，信息查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation("个人中心，信息查询")
    @GetMapping("/userPage")
    public Result<Page<OrdersDto>> page(HttpServletRequest request, int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        Long userId = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, userId);
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(ordersPage, queryWrapper);

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<Orders> ordersRecords = ordersPage.getRecords();

        List<OrdersDto> OrdersDtoRecords = ordersRecords.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            LambdaQueryWrapper<OrderDetail> detailQueryWrapper = new LambdaQueryWrapper<>();
            detailQueryWrapper.eq(OrderDetail::getOrderId, item.getNumber());
            List<OrderDetail> list = orderDetailService.list(detailQueryWrapper);
            ordersDto.setOrderDetail(list);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(OrdersDtoRecords);

        return Result.success(ordersDtoPage);
    }

    /**
     * 管理端显示订单详情
     *
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("管理端显示订单详情")
    public Result<Page> page(int page, int pageSize, Long number) {
        // 分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(number != null, Orders::getId, number);
        //添加排序条件
        queryWrapper.orderByAsc(Orders::getCheckoutTime);
        //执行查询
        orderService.page(pageInfo, queryWrapper);

        List<Orders> records = pageInfo.getRecords();

        records.forEach(item -> {
            item.setUserName(item.getConsignee());
        });

        return Result.success(pageInfo);
    }

    /**
     * 修改订单的状态  订单状态
     * 1待付款
     * 2待派送
     * 3已派送
     * 4已完成
     * 5已取消
     *
     * @param order
     * @return
     */
    @PutMapping
    @ApiOperation("修改订单的状态")
    public Result<String> put(@RequestBody Orders order) {
        log.info("order{}", order);
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getId, order.getId()).set(Orders::getStatus, order.getStatus());

        orderService.update(updateWrapper);
        return Result.success("修改成功");

    }

}